package comm

import "time"

func MinInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// TryFn is the retry function signature.
type TryFn func() bool

func Retry(max uint16, fn TryFn) {
	for i := uint16(1); i <= max; i++ {
		if ok := fn(); ok {
			return
		}
	}
}

func DelayedRetry(delay []time.Duration, fn TryFn) {
	for _, d := range delay {
		time.Sleep(d)
		if ok := fn(); ok {
			return
		}
	}
}

func NilString(i interface{}) string {
	if i == nil {
		return ""
	}
	return i.(string)
}
